package com.bwsoft.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Service;

import com.bwsoft.domain.DictionaryData;
import com.bwsoft.domain.Fun;
import com.bwsoft.domain.User;
import com.bwsoft.domain.UserGroup;
import com.bwsoft.model.ComBoBox;
import com.bwsoft.model.Message;
import com.bwsoft.service.SystemService;

@SuppressWarnings("rawtypes")
@Service(SystemService.NAME)
public class SystemServiceImpl extends BaseServiceImpl implements SystemService {

	private static Logger log = Logger.getLogger(SystemServiceImpl.class.getClass());

	@SuppressWarnings("unchecked")
	@Override
	public Message getUserMenu(User user) {
		log.info("开始获取用户的系统菜单");
		message = new Message();
		request = ServletActionContext.getRequest();
		session = request.getSession();
		List<Fun> funs = getUserFunList(user);
		
		//获取菜单的HTML代码，用于输出到前台
		String menuHTML=getUserFunHtml(funs);
		message.setMsgStr(menuHTML);
		session.setAttribute("userMenu", menuHTML);
		log.debug("当前登录用户的菜单为：" +menuHTML);
		return message;
	}

	/**
	 * 得到用户的功能的集合
	 */
	public List<Fun> getUserFunList(User user) {
		List<Fun> funs = new ArrayList<Fun>();
		user=userDao.findById(user.getUserID());
		// 用户对应的菜单权限
		for (Fun fun : user.getFuns()) {
			fun.setType(1);
			funs.add(fun);
		}
		// 用户组对应的菜单权限
		for (UserGroup userGroup : user.getUserGroups()) {

			for (Fun fun : userGroup.getFuns()) {
				fun.setType(userGroup.getGroupID());
				funs.add(fun);
			}
		}

		Collections.sort(funs, new Comparator<Fun>() {
			@Override
			public int compare(Fun f1, Fun f2) {
				return f1.getOrderNum().compareTo(f2.getOrderNum());
			}

		});

		return funs;
	}

	
	/**
	 * 得到用户对应菜单的html
	 * 
	 * @param funs
	 * @return
	 */
	public String getUserFunHtml(List<Fun> funs) {
		StringBuffer sb = new StringBuffer();
		for (Fun fun : funs) {
			if (fun.getParentFunID()==0) {
				
				Boolean isSon = hasSonTree(fun.getFunID(), funs);
				// 组织HTML
				String son = isSon ? "class='dropdown-toggle'" : "";
				sb.append("<li>");
				sb.append("<a href='" + fun.getFunUrl()+"?fun.funName="+fun.getFunName() + "' " + son + " target='cont'>");
				sb.append("<i class='" + fun.getFunIcon() + "'></i>");
				sb.append("<span class='menu-text'>" + fun.getFunName() + "</span>");
				sb.append("<b class='arrow icon-angle-down'></b>");
				sb.append("</a>");
				// 如果该节点下仍有子节点开始递归
				if (isSon) {
					sb = getSonTree(fun.getFunID(), funs, sb);
				}
				sb.append("</li>");
				
			}
		}
		return sb.toString();
	}

	private StringBuffer getSonTree(Integer funID, List<Fun> funs, StringBuffer sb) {

		sb.append("<ul class=\"submenu\">");
		for (Fun fun : funs) {
			if (fun.getParentFunID() .equals( funID)) {
			
				Boolean isSon = hasSonTree(fun.getFunID(), funs);
				// 开始组织HTML
				String son = isSon ? "class='dropdown-toggle'" : "";
				sb.append("<li>");
				sb.append("<a href='" + fun.getFunUrl()+"?fun.funName="+fun.getFunName() +  "'" + son + " target='cont'>");
				if (fun.getLevelNum() < 3)
					sb.append("<i class='icon-double-angle-right'></i>");
				sb.append(fun.getFunName());
				if (isSon)
					sb.append("<b class='arrow icon-angle-down'></b>");
				sb.append("</a>");
				// 其下是否有子元素，如果有责继续递归操作
				if (isSon) {
					sb =  getSonTree(fun.getFunID(), funs, sb);
				}
				sb.append("</li>");
				
			}
		}
		sb.append("</ul>");

		return sb;
	}

	/**
	 * 判断此节点下是否有子节点
	 * 
	 * @param funID
	 * @param funs
	 * @return
	 */
	private boolean hasSonTree(Integer funID, List<Fun> funs) {
		Boolean flag = false;
		for (Fun fun : funs) {
			if (fun.getParentFunID().equals(funID)) {
				flag = true;
				break;
			}
		}
		return flag;
	}

	@Override
	public List<ComBoBox> getDictionary(ComBoBox comBoBox) {
		List<ComBoBox> coms=new ArrayList<ComBoBox>();
		List<DictionaryData> dics=dictionaryDataDao.findByHql("from DictionaryData as dic where dic.dictionary.dicName='"+comBoBox.getName()+"' and dic.status=1");
		
		if(comBoBox.getIsSelect()!=null&&comBoBox.getIsSelect()){
			ComBoBox com=new ComBoBox();
			com.setKey("");
			com.setValue("==请选择==");
			coms.add(com);
		}
		
		for(DictionaryData dic:dics){
			ComBoBox com=new ComBoBox();
			com.setKey(dic.getDicKey());
			com.setValue(dic.getDicValue());
			coms.add(com);
		}
		return coms;
	}

}
